package com.dodol.util;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.ChartLocation;
import com.aventstack.extentreports.reporter.configuration.Theme;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;


/**
 * @Auther: 80736
 * @Date: 2018/9/4 13:17
 * @Description:
 */

public class ExtentTestNGITestListener implements ITestListener {

    private static ExtentReports extent = ExtentManager.createInstance("extent.html");
    private static ThreadLocal parentTest = new ThreadLocal();
    private static ThreadLocal test = new ThreadLocal();


    public synchronized void onStart(ITestContext context) {
        ExtentTest parent = extent.createTest(getClass().getName());
        parentTest.set(parent);
    }


    public synchronized void onFinish(ITestContext context) {
        extent.flush();
    }


    public synchronized void onTestStart(ITestResult result) {
//        ExtentTest child = parentTest.get().createNode(result.getMethod().getMethodName());
//        test.set(child);
    }


    public synchronized void onTestSuccess(ITestResult result) {
        ((ExtentTest) test.get()).pass("Test passed");
    }


    public synchronized void onTestFailure(ITestResult result) {
        ((ExtentTest) test.get()).fail(result.getThrowable());
    }


    public synchronized void onTestSkipped(ITestResult result) {
        ((ExtentTest) test.get()).skip(result.getThrowable());
    }


    public synchronized void onTestFailedButWithinSuccessPercentage(ITestResult result) {

    }
}
